package org.jeecg.modules.demo.zkfish.datainfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.zkfish.fishfeedback.entity.FishFeedback;
import org.jeecg.modules.demo.zkfish.fishfeedback.service.IFishFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.yaml.snakeyaml.Yaml;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;

/**
* @Description: 捕鱼反馈
* @Author: jeecg-boot
* @Date:   2024-03-24
* @Version: V1.0
*/
@Api(tags="捕鱼反馈")
@RestController
@RequestMapping("/zkfish.fishfeedback/datainfo")
@Slf4j
public class DataInfoController extends JeecgController<FishFeedback, IFishFeedbackService> {

    @Value("${datainfo.infopath}")
    private String datainfoPath;

    @Value("${datainfo.baseurl}")
    private String baseurl;

    @GetMapping(value = "/get")
    public Result<Map<String, Object>> queryPageList() {
        Result<Map<String, Object>> result = new Result<>();
        Map<String, Object> yamlMap = null;
        if(datainfoPath.startsWith("http")){
            yamlMap = getYmlFromHttp(datainfoPath);
        }
        else {
            yamlMap = getYmlFromFile(datainfoPath);
        }
        yamlMap.put("baseurl",baseurl);
        result.setResult(yamlMap);
        return result;
    }

    public Map<String, Object> getYmlFromFile(String yamlPath){
        Yaml yaml = new Yaml();
        Map<String, Object> yamlMap = null;

        try (InputStream in = Files.newInputStream(Paths.get(yamlPath))) {
            yamlMap = yaml.load(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return yamlMap;
    }

    public Map<String, Object> getYmlFromHttp(String yamlUrl){
        Yaml yaml = new Yaml();
        Map<String, Object> yamlMap = null;

        try {
            URL url = new URL(yamlUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 获取响应码，检查是否成功
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                // 读取响应内容
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine+"\n");
                }
                in.close();
                yamlMap = yaml.load(response.toString());

            } else {
                System.out.println("Failed to retrieve the YAML config from the URL.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return yamlMap;
    }

}
